﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wedo.Domain.Models;
using Wedo.Recognition.Domain;
using Wedo.Mvc.Utility;
using Wedo.Mvc.Pager;
using Wedo.Recognition.Repository;
using Wedo.Recognition.Service;
using Wedo.Utility.Common;
using Wedo.Mvc.Utility.Models;

namespace Wedo.Recognition.Web.Controllers
{ 
    public class RoleController :  EntityKeyController<Role, string,SearchModel>
    {
        private RoleService service;
        private IUnitWork _dbContext;

        public RoleController(IUnitWork dbContext)
        {
            _dbContext = dbContext;
            service = new RoleService(_dbContext);
        }
      
        protected override void Dispose(bool disposing)
        {
            _dbContext.Dispose();
            base.Dispose(disposing);
        }

        protected override void Insert(Role model)
        {
            service.Add(model);
        }

        protected override void DeleteModel(string id)
        {
            service.Remove(id);
        }

        protected override void UpdateModel(Role model)
        {
            service.Update(model);

        }

        protected override Role FindById(string Id)
        {
           return service.GetById(Id);
        }

        protected override Wedo.Mvc.Pager.PagedList<Role> GetSearchPagedList(SearchModel where)
        {
            where=where??new SearchModel();
            var query = service.All();
            if (where.Keyword.IsNotNullOrWhiteSpace())
            {
                query = query.Where(m => m.Description.Contains(where.Keyword) ||
                    m.Id.Contains(where.Keyword)
                    || m.RoleName.Contains(where.Keyword));
            }
            return query.ToPagedList(where.Page,15);
        }

        public ActionResult EditPermission(string Id)
        {
            var role = this.FindById(Id);
            var res = role.Resources;
            var list= _dbContext.CacheSet<Resource>()
                .CachedAll().GroupBy(m=>m.Module_Id);
            var Modules = _dbContext.CacheSet<Module>().CachedAll();

            List<CommonGroup<string, GroupSelect>> sl = new List<CommonGroup<string, GroupSelect>>();
            foreach (var item in list) {
                var module = Modules.Find(m=>m.Id==item.Key);
                CommonGroup<string, GroupSelect> li = new CommonGroup<string, GroupSelect>(module.Name);
                foreach (var m in item)
                {
                    bool selected = res.Any(s => s.Id == m.Id)  ;

                    li.Add(new GroupSelect { Text = m.Name, Value = m.Id.ToString(), Selected = selected, Group=m.Controller });
                }
                sl.Add(li);
            } 
            ViewData["Resources"] = sl;

            return ReturnAjaxView(role);
        }

        [HttpPost]
        public ActionResult EditPermission(string Id, List<Guid> Resources)
        {
            try
            {
                Role role = FindById(Id);
                role.CheckNull();

                if (role.Resources != null && role.Resources.Count > 0)
                {
                    role.Resources.Clear();
                }
                if (Resources != null && Resources.Count > 0)
                {
                    var res = _dbContext.Set<Resource>().Filter
                        (m => Resources.Any(s => s == m.Id)).ToList();
                    role.Resources = res;
                }
                _dbContext.SaveChanges();
                return SuccessMsg("保存成功！");
            }
            catch (Exception ex)
            {
                return ErrorMSG(ex.Message);
            }
        }
    }
}